package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /*
     * 新增购物车
     * */
    @Override
    public void add(ShoppingCartDTO dto) {
        //先查询数据库中是否有数据，有就加数量
        dto.setUserId(BaseContext.getCurrentId());
        ShoppingCart shoppingCart = shoppingCartMapper.queryIsExist(dto);
        if (shoppingCart != null) {
            // 购物车以后信息 数量+1
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateByCondition(shoppingCart);
        } else {
            //添加数据
            //判断是菜品还是套餐、
            if (dto.getDishId() != null) {
                //菜品 查询菜品信息并保存
                Dish dish = dishMapper.queryById(dto.getDishId());
                shoppingCart = ShoppingCart.builder()
                        .number(1)
                        .createTime(LocalDateTime.now())
                        .userId(BaseContext.getCurrentId())
                        .amount(dish.getPrice())
                        .image(dish.getImage())
                        .name(dish.getName())
                        .dishId(dto.getDishId())
                        .build();
                if (dto.getDishFlavor() != null) {
                    shoppingCart.setDishFlavor(dto.getDishFlavor());
                }
            } else {
                // 套餐
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart = ShoppingCart.builder()
                        .number(1)
                        .createTime(LocalDateTime.now())
                        .userId(BaseContext.getCurrentId())
                        .amount(setmeal.getPrice())
                        .image(setmeal.getImage())
                        .setmealId(dto.getSetmealId())
                        .name(setmeal.getName())
                        .build();
            }

            shoppingCartMapper.save(shoppingCart);
        }
    }

    /*
     * 查看购物车
     * */
    @Override
    public List<ShoppingCart> list(Long currentId) {
        return shoppingCartMapper.list(currentId);
    }

    /*
     * 删除一个商品
     * */
    @Override
    public void subOne(ShoppingCartDTO dto) {
        dto.setUserId(BaseContext.getCurrentId());
        ShoppingCart shoppingCart = shoppingCartMapper.queryIsExist(dto);
        if (shoppingCart.getNumber() == 1) {
            shoppingCartMapper.delById(shoppingCart.getId());
        } else {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateByCondition(shoppingCart);
        }
    }

    @Override
    public void delAll(Long currentId) {
        shoppingCartMapper.delByUserId(currentId);
    }
}


